package com.chalmers.foa.util;

import com.chalmers.foa.gamemodel.gameobjects.IGameObject;

/**
 * This class may be used instead lists such as ArrayList and Vector
 * to avoid unnecessary memory allocation. It's specifically designed for
 * GameObjectAdapter.
 * 
 *
 */
public class NewList {
	
	private IGameObject[] array;
	private int iterator;
	
	public NewList(int size) {
		array = new IGameObject[size];
		iterator = 0;
	}
	
	/**
	 * Add a new GameObjectAdapter to this list.
	 * 
	 * @param object The object to add to this list.
	 */
	public void add(IGameObject object) {
		if(iterator == array.length) {
			IGameObject[] newArray = new IGameObject[array.length*2];
			for(int i = 0; i < array.length; i++) {
				newArray[i] = array[i];
			}
			array = newArray;
		}
		array[iterator] = object;
		iterator++;
	}
	/**
	 * Clears this list.
	 */
	public void clear() {
		iterator = 0;
	}
	/**
	 * Returns the size of this list
	 * 
	 * @return The size of this list as an integer.
	 */
	public int getSize() {
		return iterator;
	}
	/**
	 * Returns the object at the specified position in this list.
	 * 
	 * @param position The position of the element to return.
	 * @return The object at the specified position.
	 * @throws IndexOutOfBoundsException
	 */
	public IGameObject getObjectAt(int position) throws IndexOutOfBoundsException {
		if(position < 0 || position >= iterator)
			throw new IndexOutOfBoundsException();
		return array[position];
	}
	
}
